<?php
class AccountController extends Controller {
	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/index.php";
	}
	
	/**
	 * 登陆
	 */
	public function Login() {
		$username = $this->getRequest ()->get ( "userName" );
		$password = $this->getRequest ()->get ( "password" );
		$osType = $this->getRequest ()->get ( "osType" );
		$token = $this->getRequest ()->get ( "token" );
		$isStudent = $this->getRequest ()->get ( "isStudent" );
		if ($username && $password && $osType && $token) {
			
			$user = new uuser ();
			$userinfo = $user->authUser ( $username, $password, 0 );
			$data = array ();
			
			if ($userinfo ["result"] > 0) {
				// 判断是否从教师端登陆学生账号, 或者从学生端登陆教师账号, 此时返回登陆失败
				if (($isStudent && $userinfo ["userinfo"] ["role_id"] != 6) || (! $isStudent && $userinfo ["userinfo"] ["role_id"] == 6)) {
					$this->view->setState ( "0" );
					$this->view->setStatus ( "0" );
					$this->view->setMsg ( "登录失败!" );
					$this->view->setData ( ( object ) null );
					
					$this->view->display ( "json" );
					return;
				}
				$data ["userID"] = $userinfo ["userinfo"] ["user_id"];
				$data ["nickName"] = $userinfo ["userinfo"] ["user_nickname"];
				$data ["auth"] = $userinfo ["userinfo"] ["role_id"];
				$session = $this->getApp ()->loadUtilClass ( "SessionUtil" );
				$session->set ( "session_userid", $userinfo ["result"] );
				$user->setPhoneInfo ( $userinfo ["result"], $osType, $token == "null" ? "" : $token );
				// TODO 没有code_platform
				// if ($username == "13100000001") {
				// $data ["auth"] = "0";
				// } else if ($username == "13100000002") {
				// $data ["auth"] = "1";
				// } else if ($username == "13100000003") {
				// $data ["auth"] = "2";
				// } else {
				// $data ["auth"] = "-1";
				// }
				$this->view->setState ( "1" );
				$this->view->setStatus ( "1" );
				$this->view->setMsg ( "登录成功!" );
				$this->view->setData ( $data );
				$this->view->display ( "json" );
				
				$log = new ulog ();
				if ($userinfo ["userinfo"] ["role_id"] == 6)
					$log->addUlog ( $userinfo ["result"], 1, true );
				else
					$log->addUlog ( $userinfo ["result"], 21, true );
			} else {
				
				if ($userinfo ["result"] == - 1) {
					
					$this->view->setState ( "0" );
					$this->view->setStatus ( "0" );
					$this->view->setData ( ( object ) null );
					
					$this->view->setMsg ( "密码错误!" );
					$this->view->display ( "json" );
				} else {
					$this->view->setState ( "0" );
					$this->view->setStatus ( "0" );
					$this->view->setData ( ( object ) null );
					
					$this->view->setMsg ( "用户不存在!" );
					$this->view->display ( "json" );
				}
			}
		} else {
			$this->view->setState ( "0" );
			$this->view->setMsg ( "参数缺失!" );
			$this->view->setStatus ( "0" );
			$this->view->setData ( ( object ) null );
			
			$this->view->display ( "json" );
		}
	}
	
	/**
	 * 注销
	 */
	public function Logout() {
		if ($userInfo = $this->getData ( "userinfo" )) {
			$user = new uuser ();
			$user->delPhoneInfo ( $userInfo ["user_id"] );
		}
		$userinfo = $this->getData ( 'userinfo' );
		$log = new ulog ();
		if ($userinfo ["role_id"] == 6)
			$log->addUlog ( $userinfo ["user_id"], 2, true );
		else
			$log->addUlog ( $userinfo ["user_id"], 22, true );
		
		$session = $this->getApp ()->loadUtilClass ( "SessionUtil" );
		$session->clear ();
		$this->view->setMsg ( "logout succeed!" );
		$this->view->setState ( "1" );
		$this->view->setStatus ( "0" );
		$this->view->setData ( ( object ) null );
		
		$this->view->display ( "json" );
	}
	
	/**
	 * 生成验证码, 用于找回密码第一步
	 */
	public function Getverifycode() {
		$phoneNum = $this->getRequest ()->get ( "phoneNum" );
		if ($phoneNum) {
			$session = $this->getApp ()->loadUtilClass ( "SessionUtil" );
			// 生成验证码
			list ( $msec, $sec ) = explode ( ' ', microtime () );
			srand ( $sec );
			$code = rand ( 1000, 9999 );
			$session->set ( "code", $code );
			
			$stringUtil = $this->getApp ()->loadUtilClass ( "StringUtil" );
			if ($stringUtil->is_phone ( $phoneNum )) {
				$user = new uuser ();
				$userinfo = $user->getUserFromPhone ( $phoneNum );
				if ($userinfo) {
					$sms = $this->getApp ()->loadConf ( "sms" );
					$content = "您的验证码是:" . $code . "。五分钟内有效，感谢您使用好度科技软件，我们的网址是howdoo.cn".$sms ['sign'];
					$smsData = array (
							"username" => $sms ['ZH'],
							"password" => $sms ['MM'],
							"mobile" => $phoneNum,
							"content" => $content
					);
					$curl = $this->getApp ()->loadUtilClass ( "CurlUtil" );
					$result = $curl->post ( $sms ['url'], $smsData );
					if ($result ["state"] == true && $result ["info"] == '0') { // 发送成功
						$this->view->setState ( "1" );
						$this->view->setData ( array (
								"isUserReg" => "1" 
						) );
						$this->view->setMsg ( "信息发送成功!" );
						$this->view->display ( "json" );
					} else {
						$this->view->setState ( "0" );
						$this->view->setData ( array (
								"isUserReg" => "0" 
						) );
						$this->view->setMsg ( "信息发送失败!(" . $result ["code"] . ":" . $result ["info"] . ")" );
						$this->view->display ( "json" );
					}
				} else {
					$this->view->setState ( "0" );
					$this->view->setData ( array (
							"isUserReg" => "0" 
					) );
					$this->view->setMsg ( "账号不存在" );
					$this->view->display ( "json" );
				}
			} else {
				// phoneNum参数不是手机
				$this->view->setMsg ( "参数格式错误" );
				$this->view->setState ( "0" );
				$this->view->setStatus ( "0" );
				$this->view->setData ( ( object ) null );
				
				$this->view->display ( "json" );
			}
		} else {
			// 没有phoneNum参数
			$this->view->setMsg ( "参数缺失" );
			$this->view->setState ( "0" );
			$this->view->setStatus ( "0" );
			$this->view->setData ( ( object ) null );
			
			$this->view->display ( "json" );
		}
	}
	
	/**
	 * 核对验证码
	 */
	public function Checkverifycode() {
		$phoneNum = $this->getRequest ()->get ( "phoneNum" );
		$verifyCode = $this->getRequest ()->get ( "verifyCode" );
		if ($phoneNum && $verifyCode) {
			
			$session = $this->getApp ()->loadUtilClass ( "SessionUtil" );
			$code = $session->get ( "code" );
			
			if ($code == $verifyCode) {
				$session->set ( "findpw_account", $phoneNum );
				$this->view->setState ( "1" );
				$this->view->setStatus ( "0" );
				$this->view->setMsg ( "验证码正确!" );
			} else {
				$this->view->setState ( "0" );
				$this->view->setStatus ( "0" );
				$this->view->setData ( ( object ) null );
				
				$this->view->setMsg ( "验证码错误!" );
			}
			$this->view->display ( "json" );
		} else {
			// 没有phoneNum, verifyCode参数
			$this->view->setMsg ( "参数缺失" );
			$this->view->setState ( "0" );
			$this->view->setStatus ( "0" );
			$this->view->setData ( ( object ) null );
			
			$this->view->display ( "json" );
		}
	}
	/**
	 * 手机注册
	 */
	public function Mobileregister() {
		$phoneNum = $this->getRequest ()->get ( "phoneNum" );
		$password = $this->getRequest ()->get ( "password" );
		$nickName = $this->getRequest ()->get ( "nickName" );
		$subjectID = $this->getRequest ()->get ( "subjectID" );
		$osType = $this->getRequest ()->get ( "osType" );
		$Token = $this->getRequest ()->get ( "Token" );
		$isStudent = $this->getRequest ()->get ( "isStudent" );
		if (! ($phoneNum && $password && $nickName && subjectID && $osType && $Token)) {
			$this->view->setMsg ( "参数缺失" );
			$this->view->setState ( "0" );
			$this->view->setStatus ( "0" );
			$this->view->setData ( ( object ) null );
			
			$this->view->display ( "json" );
			exit ();
		}
		$stringUtil = $this->getApp ()->loadUtilClass ( "StringUtil" );
		if (! $stringUtil->is_phone ( $phoneNum )) {
			// phoneNum参数不是手机
			$this->view->setMsg ( "参数格式错误" );
			$this->view->setState ( "0" );
			$this->view->setStatus ( "0" );
			$this->view->setData ( array (
					"isUserReg" => 0,
					"userID" => "-2" 
			) );
			$this->view->display ( "json" );
			exit ();
		}
		if ($isStudent) {
			$roleid = 6;
		} else {
			$roleid = 2;
		}
		$user = new uuser ();
		$result = $user->addUser ( $phoneNum, $password, $subjectID, $nickName, $roleid );
		if ($result == - 3) {
			$this->view->setMsg ( "该手机号码已经注册为学生用户，请直接登录" );
		} else if ($result == - 4) {
			$this->view->setMsg ( "该手机号码已经注册为教师用户，请直接登录" );
		} else if ($result == - 2) {
			$this->view->setMsg ( "不是一个合法的手机号" );
		} else if ($result == - 1) {
			$this->view->setMsg ( "无法使用该手机号注册" );
		}
		if ($result <= 0) {
			
			$this->view->setState ( "0" );
			$this->view->setStatus ( "0" );
			$this->view->setData ( array (
					"isUserReg" => 0,
					"userID" => $result 
			) );
			$this->view->display ( "json" );
		} else {
			// 注册成功
			// $user->setPhoneInfo ( $result, $osType, $Token );//TODO 是否需要添加设备信息??
			$session = $this->getApp ()->loadUtilClass ( "SessionUtil" );
			$session->set ( "session_userid", $result );
			$user->setPhoneInfo ( $result, $osType, $Token == "null" ? "" : $Token );
			$this->view->setStatus ( "1" );
			$this->view->setData ( array (
					"isUserReg" => 1,
					"userID" => $result 
			) );
			$this->view->setState ( "1" );
			$this->view->setMsg ( "注册成功!" );
			$this->view->display ( "json" );
		}
	}
	/**
	 * 重置密码
	 */
	public function Resetpassword() {
		$phoneNum = $this->getRequest ()->get ( "phoneNum" );
		$password = $this->getRequest ()->get ( "password" );
		$session = $this->getApp ()->loadUtilClass ( "SessionUtil" );
		if (! ($phoneNum && $password)) {
			// $session->del ( "findpw_account" );
			$this->view->setMsg ( "参数缺失" );
			$this->view->setState ( "0" );
			$this->view->setStatus ( "0" );
			$this->view->setData ( ( object ) null );
			
			$this->view->display ( "json" );
			exit ();
		}
		if (! $session->get ( "findpw_account" )) {
			$this->view->setMsg ( "访问错误" );
			$this->view->setState ( "0" );
			$this->view->setStatus ( "0" );
			$this->view->setData ( ( object ) null );
			
			$this->view->display ( "json" );
		} else {
			$user = new uuser ();
			$userinfo = $user->getUserFromPhone ( $phoneNum );
			if (! $userinfo) {
				$this->view->setMsg ( "账号错误" );
				$this->view->setState ( "0" );
				$this->view->setStatus ( "0" );
				$this->view->setData ( ( object ) null );
				
				$this->view->display ( "json" );
			} else {
				$log = new ulog ();
				if ($user->setPw ( $userinfo ['user_id'], $password )) {
					$this->view->setMsg ( "密码重置成功!" );
					$this->view->setState ( "1" );
					$this->view->setStatus ( "0" );
					$this->view->setData ( ( object ) null );
					if ($userinfo ["role_id"] == 6)
						$log->addUlog ( $userinfo ["user_id"], 6, true );
					else
						$log->addUlog ( $userinfo ["user_id"], 25, true );
				} else {
					$this->view->setMsg ( "密码重置失败!" );
					$this->view->setState ( "0" );
					$this->view->setStatus ( "0" );
					$this->view->setData ( ( object ) null );
					if ($userinfo ["role_id"] == 6)
						$log->addUlog ( $userinfo ["user_id"], 6, false );
					else
						$log->addUlog ( $userinfo ["user_id"], 25, false );
				}
				$this->view->display ( "json" );
			}
		}
	}
	/**
	 * 用于提供新版本客户端介绍和下载地址
	 *
	 * @author linkai
	 */
	public function getNewVersion() {
		$platformName = $this->getRequest ()->get ( 'osType' );
		$versionNum = $this->getRequest ()->get ( 'version' );
		$version = new version ();
		$array = array ();
		
		if ($platformName) {
			if ($versionNum) {
				$versionInfo = $version->getLastVersion ( $platformName, $versionNum );
				if ($versionInfo) {
					$data = array (
							"version" => $versionInfo [0] ['version_num'],
							"intro" => $versionInfo [0] ['version_desc'],
							"url" => $versionInfo [0] ['upgrade_url'] 
					);
					$this->view->setMsg ( "获取新版本成功！" );
				} else {
					$data = ( object ) null;
					$this->view->setMsg ( "当前版本已经是最新版本！" );
				}
				$this->view->setState ( "1" );
			} else {
				$data = ( object ) null;
				$this->view->setState ( "0" );
				$this->view->setMsg ( "未提交版本号！" );
			}
		} else {
			$data = ( object ) null;
			$this->view->setState ( "0" );
			$this->view->setMsg ( "未提交平台号！" );
		}
		$this->view->setData ( $data );
		$this->view->display ( "json" );
	}
	/**
	 * 修改密码
	 *
	 * @author linkai
	 */
	public function Updatepassword() {
		$oldPassword = $this->getRequest ()->get ( "oldPassword" );
		$password = $this->getRequest ()->get ( "password" );
		$session = $this->getApp ()->loadUtilClass ( "SessionUtil" );
		
		if (! ($oldPassword && $password)) {
			// $session->del ( "findpw_account" );
			$this->view->setMsg ( "参数缺失" );
			$this->view->setState ( "0" );
			$this->view->setStatus ( "0" );
			$this->view->setData ( ( object ) null );
			
			$this->view->display ( "json" );
			exit ();
		}
		// if (! $session->get ( "findpw_account" )) {
		// $this->view->setMsg ( "访问错误" );
		// $this->view->setState ( "0" );
		// $this->view->setStatus ( "0" );
		// $this->view->setData ( (object) null);
		
		// $this->view->display ( "json" );
		// exit ();
		// }
		// 获取用户ID
		$userinfo = $this->getData ( 'userinfo' );
		$userID = $userinfo ['user_id'];
		// 旧密码是否正确
		$user = new uuser ();
		// $result = $user->modifyPassword ( $userID, $oldPassword, $password );
		// $userinfo = $user->getAdminFromAdminid ( $userID );
		$log = new ulog ();
		if ($userinfo) {
			if ($userinfo ['user_pw'] == $user->generatePw ( $oldPassword, $userinfo ['user_salt'] )) {
				if ($user->setPw ( $userID, $password )) {
					$this->view->setState ( "1" );
					$this->view->setMsg ( "修改成功" );
					if ($userinfo ["role_id"] == 6)
						$log->addUlog ( $userinfo ["user_id"], 5, true );
					else
						$log->addUlog ( $userinfo ["user_id"], 24, true );
				} else {
					$this->view->setState ( "0" );
					$this->view->setMsg ( "修改失败" );
					if ($userinfo ["role_id"] == 6)
						$log->addUlog ( $userinfo ["user_id"], 5, false );
					else
						$log->addUlog ( $userinfo ["user_id"], 24, false );
				}
			} else {
				$this->view->setState ( "0" );
				$this->view->setMsg ( "密码错误，请正确提交原密码！" );
				if ($userinfo ["role_id"] == 6)
					$log->addUlog ( $userinfo ["user_id"], 5, false );
				else
					$log->addUlog ( $userinfo ["user_id"], 24, false );
			}
			$this->view->setStatus ( "1" );
		} else {
			$this->view->setStatus ( "0" );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "用户不存在，请重新登陆！" );
		}
		$this->view->setData ( ( object ) null );
		
		$this->view->display ( "json" );
	}
}